R/plot rfu.R

Defines functions plot_rfu

# May 7, 2019

#'@title Plot fluorescence versus time by well
#'@description This function plots fluorescence versus time for each well on a well plate following a sinking assay. The goal is to simply visualize the data; no data transformation or model fitting happens here.
#'@usage plot_rfu(database_rfu, size = 3, shape = 6, colour = "darkgreen", fill = "white",
#'export_output = FALSE, save_as = "Sinking_RFU.pdf")
#'@param database_rfu dataframe containing the columns "Plate", "Well", "Elapsed.Time.m", and "RFU". Can be generated by the user, or by running the import functions on raw data files.
#'@param size point size.
#'@param shape point shape.
#'@param colour point outline colour.
#'@param fill point fill colour.
#'@param export_output whether resulting plots should be saved to a .pdf file. Defaults to FALSE; when FALSE, plots can be viewed within RStudio.
#'@param save_as desired filename, if results are to be exported.
#'@return Plots of fluorescence (RFU) versus elapsed time for each well on a well plate. Either exported to .pdf or appear in the plot viewer.
#'@examples
#' plot_rfu(database_rfu, size = 4, shape = 1, colour = "blue", export_output = TRUE, save_as = "Sinking_Plots.pdf")

# ----------------------------------------------------------------------------------

plot_rfu <- function(database_rfu, size = 3, shape = 6, colour = "darkgreen", fill = "white",
                     export_output = FALSE, save_as = "Sinking_RFU.pdf") {

# ----------------------------------------------------------------------------------

  plot_by <- paste(database_rfu$Plate, database_rfu$Well)

  wells <- unique(plot_by)

  if (export_output == TRUE) {

    pdf(save_as)

    } # open .pdf, only if plots are to be exported

  for (w in 1:length(wells)) {

    focus_well <- wells[w]

    well_data <- dplyr::filter(database_rfu, plot_by == focus_well)

    p <- ggplot2::ggplot(well_data, ggplot2::aes(Elapsed.Time.m, RFU)) +
      ggplot2::geom_point(size = size, shape = shape, colour = colour, fill = fill) +
      ggplot2::theme_classic() +
      ggplot2::labs(x = "Elapsed time (minutes)", y = "RFU", title = well_data$Well) +
      ggplot2::theme(axis.title = ggplot2::element_text(size = 16, colour = "black", face = "bold")) +
      ggplot2::theme(axis.text = ggplot2::element_text(size = 16, colour = "black")) +
      ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5, size = 18, face = "bold"))

    print(p)

    } # close for loop

  if (export_output == TRUE) {

    dev.off()

    } # close .pdf if it was opened

  } # End of function
mlrioux/sinkworx documentation built on June 2, 2020, 10:28 p.m.